package com.sf.pathmemorymethod;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.SyncStateContract;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.sf.util.Constant;
import com.sf.util.Mysqlitehelper;
import com.sf.util.Path;

/**
 * Created by liutaigang on 2018/7/7.
 */

public class NumberDefineFixActivity extends Activity {

    private Mysqlitehelper helper;
    private Button btnCancel;
    private Button btnSave;
    private EditText description;
    private TextView number;
    private int id;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_num_define_fix);

        //初始化控件
        btnCancel = (Button) findViewById(R.id.num_define_fix_cancel);
        btnSave = (Button) findViewById(R.id.num_define_fix_save);
        description = (EditText) findViewById(R.id.num_define_fix_description);
        number = (TextView) findViewById(R.id.num_define_fix_number);

        //设置数据
        Intent intent = getIntent();
        id = intent.getIntExtra("id", 0);
        //Toast.makeText(NumberDefineFixActivity.this, "id = "+ id, Toast.LENGTH_LONG).show();
        setData(id);

        //点击事件：取消
        btnCancel.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                NumberDefineFixActivity.this.finish();
            }
        });

        //点击事件：更新
        btnSave.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {

                //更新数据
                helper = new Mysqlitehelper(NumberDefineFixActivity.this);
                SQLiteDatabase db = helper.getWritableDatabase();
                ContentValues values = new ContentValues();
                values.put(Constant.NAME, description.getText().toString().trim()); //键值对 put(字段名称,值
                String[] args = {String.valueOf(id)};
                long result = db.update(Constant.TABLE_NUM, values, "id=?", args);

                if (result>0){
                    Toast.makeText(NumberDefineFixActivity.this,"数据修改成功",Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(NumberDefineFixActivity.this,"数据修改失败",Toast.LENGTH_SHORT).show();
                }
                db.close();

                //刷新上一界面
                Intent intent = new Intent();
                intent.setAction("action.NumberDefineActivity");
                sendBroadcast(intent);

                //返回上一级
                NumberDefineFixActivity.this.finish();
            }
        });
    }

    private void setData(Integer id) {
        // 查询数据
        helper = new Mysqlitehelper(NumberDefineFixActivity.this);
        SQLiteDatabase db = helper.getWritableDatabase();
        String selection = "id=?";
        String[] selectionArgs = new String[]{id.toString()};
        Cursor cursor = db.query(Constant.TABLE_NUM, null, selection, selectionArgs, null, null, null);
        //Toast.makeText(NumberDefineFixActivity.this, "count_cursor = " + cursor.getColumnName(0), Toast.LENGTH_LONG).show();

        if (cursor.getCount() == 0) {
            Log.i("tag", "没有数据");
        } else {
            cursor.moveToNext();
            String name = cursor.getString(cursor.getColumnIndex(Constant.NAME));
            String num = cursor.getString(cursor.getColumnIndex(Constant.NUM));
            //Toast.makeText(NumberDefineFixActivity.this, name+"___"+num, Toast.LENGTH_LONG).show();
            number.setText(num);
            description.setText(name);
        }
        db.close();
    }
}
